package com.dzt.androidframework.jsoup.detail.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dzt.androidframework.R;
import com.dzt.androidframework.jsoup.detail.bean.ImageDetailDomain;
import com.dzt.androidframework.jsoup.utils.ColorPhrase;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;

/**
 * ============================================================
 * 项目名称：HotWXReading
 * 包名称：com.dzt.androidframework.jsoup.detail.adapter
 * 文件名：GalleryPagerAdapter
 * 类描述：
 * 创建人：flyou
 * 邮箱：fangjaylong@gmail.com
 * 创建时间：2016/3/18 9:16
 * 修改备注：
 * 版本：@version  V1.0
 * ============================================================
 **/
public class GalleryPagerAdapter extends PagerAdapter {
	private ArrayList<ImageDetailDomain> mImageDetailDomains;
	private Context mContext;

	public GalleryPagerAdapter(Context context, ArrayList<ImageDetailDomain> imageDetialDomains) {
		mImageDetailDomains = imageDetialDomains;
		mContext = context;
	}

	@Override
	public int getCount() {
		return mImageDetailDomains.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View contentView = View.inflate(mContext, R.layout.jsoup_image_gallery_item, null);
		PhotoView imageView = (PhotoView) contentView.findViewById(R.id.image);
		TextView imageDes = (TextView) contentView.findViewById(R.id.imageDesc);
		TextView pages = (TextView) contentView.findViewById(R.id.pages);
		CharSequence formatted = ColorPhrase.from("{" + (1 + position + "}/" + mImageDetailDomains.size()))
				.withSeparator("{}")
				.innerColor(0xFF00ff1d)
				.outerColor(0xFFf40c10)
				.format();
		pages.setText(formatted);
		Glide.with(mContext)
				.load(mImageDetailDomains.get(position).getImageUrl())
				.placeholder(R.drawable.pic_loading)
				.into(imageView);
		imageDes.setText(mImageDetailDomains.get(position).getImageDesc());
		container.addView(contentView);
		return contentView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
}
